package qq.activity.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.imu.appdevexperiment2022.R;


public class CchHomeFragment extends Fragment {

   CchMainActivity fragmentMainActivity;
   private Context context;
   private static final String TAG = "CchHomeFragment";
   @BindView(R.id.home_rec)
   RecyclerView recyclerView;
   @BindView(R.id.tv_left_select_text)
   TextView tv_left_select_text;


   private List<ListResultBean.DataBean> list1 =new ArrayList<ListResultBean.DataBean>();
   private List<GetByUserNameResultBean.DataBean> list2 =new ArrayList<GetByUserNameResultBean.DataBean>();
//private List<Fruit> fruitList =new ArrayList<>();
   @Override
   public void onAttach(Context context) {
      super.onAttach(context);
      this.context = context;
      fragmentMainActivity = (CchMainActivity) context;

   }

   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_home, container, false);
      ButterKnife.bind(this, view);


      init();

      return view;
   }

   private void init() {
      initData();
   }

   private void initData() {
      Map<String,String> map1=new HashMap<>();
      map1.put("classesId","130");
//      map1.put("")

      Call<ListResultBean> call = ApiClient.getInstance(context.getApplicationContext()).getReturnData(AuthUtil.GetAuth(),map1);

      call.enqueue(new Callback<ListResultBean>() {
         @Override
         public void onResponse(Call<ListResultBean> call, Response<ListResultBean> response) {
            if (null!=response.body()){

               if (200==response.body().getCode()){
                  Log.e(TAG,response.body().getMsg());

                  list1.addAll(response.body().getData());
                  Log.e(TAG,list1.get(0).getNickName());
                  initAdapter();
               }else{

               }
            }else{

            }
         }


         @Override
         public void onFailure(Call<ListResultBean> call, Throwable t) {

         }
      });

   }

   private void initAdapter() {
      LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
      recyclerView.setLayoutManager(linearLayoutManager);
      ClassAdapter adapter=new ClassAdapter(list1);
      recyclerView.setAdapter(adapter);
   }
   private void initAdapter1() {
      LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
      recyclerView.setLayoutManager(linearLayoutManager);
      ClassGetUserAdapter adapter=new ClassGetUserAdapter(list2);
      recyclerView.setAdapter(adapter);
   }
   @OnClick(R.id.img_left_select)
   public void img_left_select(){
      tv_left_select_text.getText();
      Map<String,String >map=new HashMap<>();
      map.put("userName",tv_left_select_text.getText().toString());

      Call<GetByUserNameResultBean> call = ApiClient.getInstance(context.getApplicationContext()).getReturnData1(AuthUtil.GetAuth(),map);

      call.enqueue(new Callback<GetByUserNameResultBean>() {
         @Override
         public void onResponse(Call<GetByUserNameResultBean> call, Response<GetByUserNameResultBean> response) {
            if (null!=response.body()){

               if (200==response.body().getCode()){
                  Log.e(TAG,response.body().getMsg());
                  list1.clear();
                  list2.add(response.body().getData());
                  initAdapter1();
               }else{

               }
            }else{

            }
         }

         @Override
         public void onFailure(Call<GetByUserNameResultBean> call, Throwable t) {

         }
      });


   }

}
